# _*_ coding:utf-8 _*_
# @Time : 2021/9/17 17:49
# @Author : wsl
# @File : Urllib
# @Project : python  百度翻译

import urllib.request
import urllib.parse

base_url = 'https://fanyi.baidu.com/sug'

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0'
}

keyword = input('输入你要查询的单词')
data = {
    'kw': keyword
}

# post请求的参数 要编码 编码后调用encode 不拼接到url 要在请求对象的定制参数中
new_data = urllib.parse.urlencode(data).encode('utf-8')
request = urllib.request.Request(url=base_url, headers=headers, data=new_data)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
print(content)
# string -> test.json

import json


obj = json.loads(content)
print(obj)
